import axios from 'axios'
import {Message} from 'element-ui'

axios.defaults.timeout = 5000
axios.defaults.baseURL = process.env.API_BASE

axios.interceptors.request.use(config => {
  return config
}, error => {
  Message({
    message: error.message,
    type: 'error',
    duration: 5 * 1000
  })
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  if (!response.data.errorCode) {
    return response
  }

  Message({
    message: response.data.error,
    type: 'error',
    duration: 5 * 1000
  })

  return Promise.reject(new Error('error'))
}, error => {
  Message({
    message: error.message,
    type: 'error',
    duration: 5 * 1000
  })
  return Promise.reject(error)
})

export default axios
